<?php
class permit_library
{
	function __construct()
	{
		$this->CI =& get_instance();
		//$this->check_permit();    
	}
	
	function check_permit()
	{
		$module = $this->CI->uri->segment(1);
		$func = $this->CI->uri->segment(2);
		$this->CI->db->select('sys_permission_detail.*');
		$this->CI->db->where('sys_permission_detail.permission_name',$module);
		$this->CI->db->where('sys_permission_detail.function_name',$func);
		$check_fn = $this->CI->db->get('sys_permission_detail')->row();
		if($check_fn)
		{        
			$admin_id = $this->CI->session->userdata('admin_id');
			$this->CI->db->select('sys_permission_detail.*, sys_permission_admin.*');
			$this->CI->db->join('sys_permission_detail','sys_permission_detail.function_id = sys_permission_admin.function_id');
			$this->CI->db->where('sys_permission_detail.permission_name',$module);
			$this->CI->db->where('sys_permission_admin.admin_id',$admin_id);
			$this->CI->db->where('sys_permission_detail.function_name',$func);
			$check = $this->CI->db->get('sys_permission_admin')->row();
			if(!$check)
			{
				$this->CI->session->set_flashdata('message','Bạn không có quyền thực thi chức năng trên');
				redirect($this->CI->session->userdata('Url'));         
			}else{
				//$this->write_logs($module,$func);
			}
		}
	}
	
	function write_logs($Modules,$Function)
	{
		$Url = $this->CI->session->userdata('Url');
		$AccountID = $this->CI->session->userdata('admin_id');
		$data = array(
			'admin_id' => $AccountID,
			'Modules' => $Modules,
			'Function' => $Function,
			'Url' => $Url,
			'LogInfo' =>$this->CI->agent->agent_string(),
			'LogIP' => $this->CI->input->ip_address(),
			'DateLog' => time()
		);
		$this->CI->db->insert('logs',$data);
	}
}
?>
